package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    SetmealDao setmealDao;
    /*
    *  reportData:{
                        reportDate:统计日期,
                        todayNewMember :今日新增会员数,
                        totalMember :会员总数,
                        thisWeekNewMember :本周新增会员数,
                        thisMonthNewMember :本月新增会员数,
                        todayOrderNumber :今日预约数,
                        todayVisitsNumber :今日到诊数,
                        thisWeekOrderNumber :本周预约数,
                        thisWeekVisitsNumber :本周到诊数(周一以后),
                        thisMonthOrderNumber :本月预约数,
                        thisMonthVisitsNumber :本月到诊数,
                        hotSetmeal :[
                            {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
                            {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
                        ]
                    }
    *
    * */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //今天日期
        String todayDate = DateUtils.parseDate2String(new Date());
        //获取本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月1号
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取本月最后一天
        String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

        //获取本周日的日期
        String thisWeekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());

        String reportDate = todayDate;
        //今日新增会员数
        long todayNewMember = memberDao.findTodayNewMember(todayDate);
        //会员总数
        long totalMember = memberDao.findTotalMember();
        //本周新增会员数
        long thisWeekNewMember = memberDao.findNewMemberCountAfterByDate(thisWeekMonday);
        //本月新增会员数
        long thisMonthNewMember = memberDao.findNewMemberCountAfterByDate(thisMonthFirstDay);
        //今日预约数
        long todayOrderNumber = orderDao.findTodayOrderNumber(todayDate);
        //todayVisitsNumber :今日到诊数
        long todayVisitsNumber = orderDao.findTodayVisitsNumber(todayDate);
        //本周预约数(周一到周日)
        long thisWeekOrderNumber = orderDao.findOrderNumberByDate(thisWeekMonday,thisWeekSunday);
        //本周到诊数(周一以后实际来了多少人,一个参数即可)
        long thisWeekVisitsNumber = orderDao.findVisitsNumberByDate(thisWeekMonday);
        //本月预约数
        long thisMonthOrderNumber = orderDao.findOrderNumberByDate(thisMonthFirstDay,thisMonthLastDay);
        //本月到诊数
        long thisMonthVisitsNumber = orderDao.findVisitsNumberByDate(thisMonthFirstDay);

        List<Map<String,Object>> mapList = setmealDao.findHostSetmeal();

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("reportDate",reportDate);
        resultMap.put("todayNewMember",todayNewMember);
        resultMap.put("totalMember",totalMember);
        resultMap.put("thisWeekNewMember",thisWeekNewMember);
        resultMap.put("thisMonthNewMember",thisMonthNewMember);
        resultMap.put("todayOrderNumber",todayOrderNumber);
        resultMap.put("todayVisitsNumber",todayVisitsNumber);
        resultMap.put("thisWeekOrderNumber",thisWeekOrderNumber);
        resultMap.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        resultMap.put("thisMonthOrderNumber",thisMonthOrderNumber);
        resultMap.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        resultMap.put("hotSetmeal",mapList);
        return resultMap;
    }
}
